﻿/// <reference path="../../Scripts/commen_js/MJ_JSComment.js" />
function no_button() {
    $("#LoginBox_two").fadeOut("fast");
    $("#LoginBox_two").css({ display: 'none' });
}
function yes_button() {
    var productid = ordermanager.handleid;
    var type;
    var typestr = $.trim($("#" + productid).val());
    type = typestr == "已配送" ? "0" : typestr == "未配送" ? "1" : typestr == "在配送" ? "2" : "";
    if (type == "") { return; }
    poststr = "{\"type\":\"handleordermanager\",\"htmldata\":\"" + type + "|" + productid.replace("xiugai", "") + "\"}";
    ordermanager.postajaxs(poststr, ordermanager.handleresult);
}


var ordermanager = {
    search: function () {
        var state = $.trim($("#dateboxs").text());
        state = state == "全部" ? "" : state == "未配送" ? "1" : state == "在配送" ? "2" : state == "已配送" ? "0" : "";
        var keyword = $.trim($("#keyword").val());
        var begintime = $.trim($("#begintime").val());
        var endtime = $.trim($("#endtime").val());
        if (!isUndefinedNullOrEmpty(begintime) && isUndefinedNullOrEmpty(endtime)) {
            alert("截至时间不能为空！");
            return;
        }
        else if (isUndefinedNullOrEmpty(begintime) && !isUndefinedNullOrEmpty(endtime)) {
            alert("起始时间不能为空！");
            return;
        }
        ordermanager.getdata("1", state + "|" + begintime + "|" + endtime + "|" + keyword);
    },
    getdata: function (indexpage, indexwhere) {
        poststr = "{\"type\":\"ordermanager\",\"pagecurrent\":\"" + indexpage + "\",\"indexwhere\":\"" + indexwhere + "\"}";
        this.postajaxs(poststr, this.showdata);
    },
    handleid:1,
    postajaxs: function (postdata, fangfa) {
        mj_ajax("../Handler1.ashx", "json", postdata, fangfa)
    },
    showdata: function (data) {
        if (data.result == "ok") {
            var record_count = data.record_count;
            var page_curent = data.page_curent;
            var datatable = data.table;
            var table = document.getElementById("tbodyList");
            $("#tbodyList").empty();
            for (var i = 0; i < datatable.length; i++) {
                var tr = document.createElement("tr");
                tr.id = datatable[i].id;
                var td1 = document.createElement("td");
                td1.innerHTML = datatable[i].product_name + datatable[i].product_type;
                var td2 = document.createElement("td");
                td2.innerHTML = datatable[i].price;
                var td3 = document.createElement("td");
                td3.innerHTML = datatable[i].amount;
                var td4 = document.createElement("td");
                td4.innerHTML = datatable[i].phone;
                var td5 = document.createElement("td");
                td5.innerHTML = datatable[i].building + "栋" + datatable[i].dorm;
                var td6 = document.createElement("td");
                td6.innerHTML = datatable[i].leave_word == null ? "" : datatable[i].leave_word;
                var td16 = document.createElement("td");
                td16.innerHTML = datatable[i].createtime.replace("T"," ");
                tr.appendChild(td1);
                tr.appendChild(td2);
                tr.appendChild(td3);
                tr.appendChild(td4);
                tr.appendChild(td5);
                tr.appendChild(td6);
                tr.appendChild(td16);
                var td7 = document.createElement("td");
                td7.id = "small"
                var input1 = document.createElement("input");
                input1.className = "btn btn-info input1";
                input1.id ="xiugai"+datatable[i].id;
                input1.onclick = function () {
                    ordermanager.xiugai(this.id);
                }
                var types=$.trim(datatable[i].sended);
                input1.value = types == "1" ? "未配送" : types == "2" ? "在配送" : types == "0" ? "已配送" : "";
                input1.type = "button";
                td7.appendChild(input1);
                tr.appendChild(td7);
                table.appendChild(tr);
            }

            //下面进行的是分页
            $("#record_shu").text(" 总记录数" + record_count);
            if (record_count % 10 != 0) {
                page_shu = record_count / 10 + 1;
                page_shu = parseInt(page_shu)
            }
            else {
                page_shu = record_count / 10;
            }
            var options = {
                totalPages: page_shu,
                numberOfPages: 5,
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "第一页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "最后一页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    var state = $.trim($("#dateboxs").text());
                    state = state == "全部" ? "" : state == "未配送" ? "1" : state == "在配送" ? "2" : state == "已配送" ? "0" : "";
                    var keyword = $.trim($("#keyword").val());
                    var begintime = $.trim($("#begintime").val());
                    var endtime = $.trim($("#endtime").val());
                    ordermanager.getdata(page, state + "|" + begintime + "|" + endtime + "|" + keyword);
                }
            }
            $('#example').bootstrapPaginator(options);

        }
        else {
            alert(data.msg)
            ;
        }

    },
    xiugai: function (productid) {
        ordermanager.handleid = productid;
        var typestr = $.trim($("#" + productid).val());
        var content = "";
        if (typestr == "未配送") {
            content = "配送";
        }
        else if (typestr == "在配送") {
            content = "已配送";
        }
        else if (typestr == "已配送") {
            content = "要删除";
        }
        else {
            alert("操作失败！");
            return false;
        }
        $("#content_two").text("确定" + content + "这个商品？");
        $("#LoginBox_two").fadeIn("fast");
        $("#LoginBox_two").css({ display: 'block' });
    },
    handleresult: function (data) {
        $("#LoginBox_two").fadeOut("fast");
        $("#LoginBox_two").css({ display: 'none' });
        if (data.result = "ok") {
            var words = data.id.split("|");
            var typestr = $.trim($("#xiugai" + words[1]).val());
            if (typestr == "已配送") {
                $("#" + words[1]).remove();
            }
            else if (typestr == "在配送") {
                $("#xiugai" + words[1]).val("已配送");
            }
            else if (typestr == "未配送") {
                $("#xiugai" + words[1]).val("在配送");
            }
            else { return;}
            layer.alert("成功修改！", {
                icon: 1,
                skin: 'layer-ext-moon'
            })
        }
    }
};

